<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="医生名称" prop="doctorName">
        <el-input v-model="queryParams.doctorName" placeholder="请输入医生名称" clearable />
      </el-form-item>
        <el-form-item label="患者名称" prop="patientName">
            <el-input v-model="queryParams.patientName" placeholder="请输入患者名称" clearable />
        </el-form-item>
      <el-form-item label="支付状态" prop="payState">
        <el-select v-model="queryParams.payState" placeholder="支付状态" clearable>
          <el-option label="启用" value="1" />
          <el-option label="停用" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="couponList">
      <el-table-column label="问诊编号" align="center" prop="recordNo" />
      <el-table-column label="医生" align="center" prop="doctorName" />
      <el-table-column label="职称" align="center" prop="title" />
      <el-table-column label="患者" align="center" prop="patientName" />

      <el-table-column label="问诊状态" align="center" prop="status" />
      <el-table-column label="症状" align="center" prop="symptoms" />
        <el-table-column label="诊费" align="center" prop="actualAmount" />
        <el-table-column label="支付状态" align="center" prop="payState" />
        <el-table-column label="支付时间" align="center" prop="payTime" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="text" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="couponForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="优惠券名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入优惠券名称" />
        </el-form-item>
        <el-form-item label="优惠券描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入优惠券描述" />
        </el-form-item>
        <el-form-item label="折扣类型" prop="discountType">
          <el-select v-model="form.discountType" placeholder="请选择折扣类型">
            <el-option label="固定金额" :value="0" />
            <el-option label="百分比" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="折扣值" prop="discountValue">
          <el-input v-model="form.discountValue" placeholder="请输入折扣值" />
        </el-form-item>
        <el-form-item label="最低消费" prop="minimumSpend">
          <el-input v-model="form.minimumSpend" placeholder="请输入最低消费金额" />
        </el-form-item>
        <el-form-item label="最大折扣" prop="maximumDiscount">
          <el-input v-model="form.maximumDiscount" placeholder="请输入最大折扣金额" />
        </el-form-item>
        <el-form-item label="有效期" prop="dateRange">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { getConsultationList } from '@/api/consultation'
import { ElMessage, ElMessageBox } from 'element-plus'

const loading = ref(false)
const showSearch = ref(true)
const total = ref(0)
const couponList = ref([])
const open = ref(false)
const title = ref('')
const dateRange = ref([])

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  title: undefined,
  status: undefined
})

const form = reactive({
  couponId: undefined,
  title: '',
  description: '',
  discountType: 0,
  discountValue: undefined,
  minimumSpend: undefined,
  maximumDiscount: undefined,
  startTime: '',
  endTime: ''
})

const rules = {
  title: [{ required: true, message: '优惠券名称不能为空', trigger: 'blur' }],
  discountType: [{ required: true, message: '请选择折扣类型', trigger: 'change' }],
  discountValue: [{ required: true, message: '折扣值不能为空', trigger: 'blur' }],
  dateRange: [{ required: true, message: '请选择有效期', trigger: 'change' }]
}

/** 查询优惠券列表 */
function getList() {
  loading.value = true
    getConsultationList(queryParams).then(response => {
    couponList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.couponId = undefined
  form.title = ''
  form.description = ''
  form.discountType = 0
  form.discountValue = undefined
  form.minimumSpend = undefined
  form.maximumDiscount = undefined
  dateRange.value = []
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.title = undefined
  queryParams.status = undefined
  handleQuery()
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加优惠券'
}

/** 修改按钮操作 */
function handleUpdate(row: any) {
  reset()
  const couponId = row.couponId
  getCoupon(couponId).then(response => {
    Object.assign(form, response.data)
    dateRange.value = [form.startTime, form.endTime]
    open.value = true
    title.value = '修改优惠券'
  })
}

/** 提交按钮 */
function submitForm() {
  if (dateRange.value && dateRange.value.length === 2) {
    form.startTime = dateRange.value[0]
    form.endTime = dateRange.value[1]
  }

  if (form.couponId) {
    updateCoupon(form).then(response => {
      ElMessage.success('修改成功')
      open.value = false
      getList()
    })
  } else {
    addCoupon(form).then(response => {
      ElMessage.success('新增成功')
      open.value = false
      getList()
    })
  }
}

/** 删除按钮操作 */
function handleDelete(row: any) {
  const couponId = row.couponId
  ElMessageBox.confirm('是否确认删除该优惠券?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(function() {
    return delCoupon(couponId)
  }).then(() => {
    getList()
    ElMessage.success('删除成功')
  })
}

onMounted(() => {
  getList()
})
</script>
